//
//  MYGAnswerDetailCell.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/29.
//

import UIKit
import QMUIKit

class MYGAnswerDetailCell: UITableViewCell {
    var action: ((Int) -> ())?
    var model: MYGCommunityDetailModel? {
        didSet {
            guard let model = model else { return }
            
            if model.answer.count > 0 {
                headImageView.sd_setImage(with: URL(string: model.doctor?.avatar ?? ""), placeholderImage: headPlaceholderImage)
                answerLabel.text = model.answer
                nameLabel.text = "\(model.doctor?.name ?? "") \(model.doctor?.title ?? "")"
                tagLabel.text = model.doctor?.hospital
                tagLabel.isHidden = false
                askButton.isHidden = true
                likeButton.isHidden = false
                starButton.isHidden = false
                
                likeButton.setImage(model.is_like == 1 ? UIImage(named: "Community/like") : UIImage(named: "Community/dislike"), for: .normal)
                likeButton.setTitle("\(model.like_count)", for: .normal)
                
                starButton.setImage(model.is_collect == 1 ? UIImage(named: "Community/star") : UIImage(named: "Community/unstar"), for: .normal)
                starButton.setTitle("\(model.collect_count)", for: .normal)

                askButton.setTitle("删除", for: .normal)

            } else {
                headImageView.image = headPlaceholderImage
                answerLabel.text = "暂无回答"
                nameLabel.text = ""
                tagLabel.isHidden = true
                askButton.isHidden = true
                likeButton.isHidden = true
                starButton.isHidden = true
            }
        }
    }
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        contentView.backgroundColor = .white
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func configUI(){
        contentView.backgroundColor = .white
        
        contentView.addSubview(backView)
        backView.addSubviews([
            headImageView,
            nameLabel,
            tagLabel,
            hospitalLabel,
            askButton,
            answerLabel,
            replyInfoLabel,
            likeButton,
            starButton
        ])
        backView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview().inset(15)
        }
        
        headImageView.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(12.5)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(headImageView.snp.top).offset(-3)
            make.left.equalTo(headImageView.snp.right).offset(8)
            make.height.equalTo(18.5)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(8)
            make.bottom.equalTo(headImageView.snp.bottom).offset(-1.5)
            make.height.equalTo(15)
        }
        
        hospitalLabel.snp.makeConstraints { make in
            make.left.equalTo(tagLabel.snp.right).offset(8)
            make.centerY.equalTo(tagLabel)
        }
        
        askButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 75, height: 23))
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(headImageView)
        }
        
        answerLabel.snp.makeConstraints { make in
            make.top.equalTo(headImageView.snp.bottom).offset(16)
            make.left.right.equalToSuperview().inset(12.5)
        }
        
        replyInfoLabel.snp.makeConstraints { make in
            make.top.equalTo(answerLabel.snp.bottom).offset(16)
            make.left.equalTo(12.5)
            make.height.equalTo(16.5)
            make.bottom.equalTo(-11.5)
        }
        
        likeButton.snp.makeConstraints { make in
            make.centerY.equalTo(replyInfoLabel)
            make.right.equalTo(starButton.snp.left).offset(-14)
        }
        
        starButton.snp.makeConstraints { make in
            make.centerY.equalTo(replyInfoLabel)
            make.right.equalToSuperview().offset(-12.5)
        }
    }
    
    private lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(132, 97, 201, 0.02)
        view.layerCornerRadius = 6
        view.layerBorderColor = .rgba(132, 97, 201, 0.41)
        view.layerBorderWidth = 0.5
        return view
    }()
    private lazy var headImageView: UIImageView = {
        let imageV = UIImageView(image: headPlaceholderImage)
        imageV.layerCornerRadius = 17.5
        return imageV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = .medium(13)
        label.textColor = .rgba(44, 44, 44)
        return label
    }()
    
    private lazy var tagLabel: QMUILabel = {
        let label = QMUILabel()
        label.text = ""
        label.font = .regular(11)
        label.textColor = .rgba(132, 97, 201)
//        label.layerBorderColor  = .rgba(132, 97, 201)
//        label.layerBorderWidth = 0.5
//        label.layerCornerRadius = 2
//        label.contentEdgeInsets = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
//        label.isHidden = true
        return label
    }()
    
    private lazy var hospitalLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textColor = .rgba(128, 128, 128)
        label.font = .regular(11)
        return label
    }()
    
    private lazy var askButton: UIButton = {
        let button = UIButton(type: .custom)
        button.layerBorderColor = .rgba(132, 97, 201)
        button.layerBorderWidth = 0.5
        button.layerCornerRadius = 11.5
        button.setTitle("向他咨询", for: .normal)
        button.titleLabel?.font = .regular(13)
        button.setTitleColor(.rgba(132, 97, 201), for: .normal)
        button.addTarget(self, action: #selector(askButtonClick), for: .touchUpInside)
        button.isHidden = true
        return button
    }()
    
    private lazy var answerLabel: UILabel = {
        let label = UILabel()
        label.text = "暂无回答"
        label.font = .regular(14)
        label.textColor = .rgba(72, 68, 67)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var replyInfoLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.textColor = .rgba(117, 117, 117)
        label.font = .regular(12)
        return label
    }()
    
    
    private lazy var likeButton: QMUIButton = {
        let button = QMUIButton(type: .custom)
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 4.5
        button.titleLabel?.font = .regular(13)
        button.setTitleColor(.rgba(51, 51, 51), for: .normal)
        button.setTitle("0", for: .normal)
        button.setImage(UIImage(named: "Community/dislike"), for: .normal)
        button.isHidden = true
        button.addTarget(self, action: #selector(likeButtonClick), for: .touchUpInside)
        return button
    }()
    
    private lazy var starButton: QMUIButton = {
        let button = QMUIButton(type: .custom)
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 4.5
        button.titleLabel?.font = .regular(13)
        button.setTitleColor(.rgba(51, 51, 51), for: .normal)
        button.setTitle("0", for: .normal)
        button.setImage(UIImage(named: "Community/unstar"), for: .normal)
        button.isHidden = true
        button.addTarget(self, action: #selector(starButtonClick), for: .touchUpInside)
        return button
    }()

}

extension MYGAnswerDetailCell {
    
    @objc func askButtonClick() {
        action?(0)
    }
    
    @objc func likeButtonClick() {
        action?(1)
    }
    
    @objc func starButtonClick() {
        action?(2)
    }
}
